package com.personalkeeper;

import com.personalkeeper.StatisticsCalculator.DistanceAndTime;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import static com.personalkeeper.PersonalKeeper.DATE;

public class ShowStatsActivity extends Activity {

    private static final String TAG = ShowStatsActivity.class.toString();

    private class AsyncStatisticsLoader extends AsyncTask<Long, Void, DistanceAndTime> {

        private Dialog progressDialog;

        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(
                                 ShowStatsActivity.this,
                                 getString(R.string.calculating_statistics),
                                 getString(R.string.it_may_take_a_while),
                                 true,
                                 true,
            new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    AsyncStatisticsLoader.this.cancel(true);
                    ShowStatsActivity.this.finish();
                }
            });
        }

        @Override
        protected DistanceAndTime doInBackground(Long... params) {
            DistanceAndTime sessionDistanceAndTime =
                StatisticsCalculator.getSessionStatisticsSince(ShowStatsActivity.this, params[0]);
            return sessionDistanceAndTime;
        }

        @Override
        protected void onPostExecute(DistanceAndTime result) {
            progressDialog.dismiss();
            try {
                double distance = result.getDistance();
                long time = result.getTime(); // milliseconds
                mTotalDistanceView.setText(String.valueOf(distance));
                double speed = distance / (time / 1000.0);
                if (Double.isNaN(speed))
                    speed = 0;
                mAvgSpeedView.setText(String.format("%f %s", speed, getString(R.string.meters_per_second)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private TextView mAvgSpeedView;
    private TextView mTotalDistanceView;
    private AsyncStatisticsLoader mLoader;
    private long mSince;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stats);
        mAvgSpeedView = (TextView) findViewById(R.id.avg_speed);
        mTotalDistanceView = (TextView) findViewById(R.id.total_distance);

        Bundle extra = getIntent().getExtras();
        if (extra != null && extra.containsKey(DATE)) {
            mSince = extra.getLong(DATE);
        } else {
            Log.d(TAG, "wrong arguments in intent");
            finish();
        }

        mLoader = new AsyncStatisticsLoader();
        mLoader.execute(mSince);
    }
}
